package com.jinlejia.bbs.logic;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.http.entity.MineEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import static com.jinlejia.bbs.http.utils.HttpUtils.*;
/**
 * @author 王进 on 2019/3/31.
 */
//个人中心
public class UserInfoLogic {
    private Context context;
    public UserInfoLogic(Context context){
        this.context = context;
    }

    public void loadInfo(String pin,final InfoLogicCallBack callBack){
        JSONObject json = new JSONObject();
        json.put("pin",pin);
        httpCallForm(BBSConfig.minePage,json).execute(httpCallBack(context, new HttpCallBack() {
            @Override
            public void onError(Exception e) {
                callBack.onResult(null);
            }

            @Override
            public void onResponse(JsonParse parse) {
                MineEntity entity = parse.parse(MineEntity.class);
                BBSUserBean user = new BBSUserBean();
                user.setUserName(entity.getUserName());
                user.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl()));
                if (!TextUtils.isEmpty(entity.getUserDeptName())){
                    user.setUserAddress(entity.getUserDeptName());
                }else {
                    user.setUserAddress(entity.getDuty());
                }
                user.setAttitudesCount(entity.getAttitudes());  //获赞数量
                user.setAttentionCount(entity.getAttention()); //关注数量
                user.setAttentionedCount(entity.getAttentioned());  //被关注数量-粉丝数量
                int isUser = entity.getIsUser();
                user.setSelf(isUser==1); //设置是否是自己的主页
                int isMutual = entity.getIsMutual();
                if (isMutual == 1){ //未关注
                    user.setAttentionType(0);
                }else if (isMutual==2){ //已关注
                    user.setAttentionType(1);
                }else if (isMutual==3){ //互相关注
                    user.setAttentionType(2);
                }

                callBack.onResult(user);
            }

            @Override
            public void onNoResponse(int code) {
                callBack.onResult(null);
            }
        }));
    }

    public interface InfoLogicCallBack{
        void onResult(BBSUserBean userBean);
    }
}
